<template>
    <el-form
        :model="shangjia"
        label-position="right"
        label-width="100px"
        class="add-shop-form"
    >
        <el-row :gutter="20">
            <el-col :span="20">
                <el-form-item label="用户id" prop="yhid">
                    <el-input
                        v-model="shangjia.yhid"
                        placeholder="请输入用户id"
                        clearable
                    />
                </el-form-item>
            </el-col>
            <el-col :span="20">
                <el-form-item label="商家名" prop="name">
                    <el-input
                        v-model="shangjia.name"
                        placeholder="请输入商家名"
                        clearable
                    />
                </el-form-item>
            </el-col>
            <el-col :span="20">
                <el-form-item label="地址" prop="address">
                    <el-input
                        v-model="shangjia.address"
                        placeholder="请输入地址"
                        clearable
                    />
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item label="起送费" prop="qsf">
                    <el-input
                        v-model="shangjia.qsf"
                        clearable
                    />
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item label="配送费" prop="psf">
                    <el-input
                        v-model="shangjia.psf"
                        clearable
                    />
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item label="打包费" prop="dbf">
                    <el-input
                        v-model="shangjia.dbf"
                        clearable
                    />
                </el-form-item>
            </el-col>
            <!-- 调整后的营业状态选择框 -->
            <el-col :span="20" style="text-align: center;">
                <el-form-item label="营业状态：" prop="status">
                    <el-select v-model="shangjia.status" placeholder="请选择营业状态" style="width: 240px">
                        <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
            </el-col>
        </el-row>
    </el-form>

    <!-- 按钮区域保持在表单外 -->
    <el-row class="button-row">
        <el-col :span="24">
            <div class="button-container">
                <el-button type="primary" @click="doSave">保存</el-button>
                <el-button @click="doCancel">取消</el-button>
            </div>
        </el-col>
    </el-row>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from "element-plus";
import axios from 'axios';

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits(["closeDialog", "flushPage"]);

const shangjia = ref({
    yhid: '',
    name: '',
    address: '',
    qsf: '',
    psf: '',
    dbf: '',
    status: '营业中'
});

const options = [
    {
        value: '营业中'
    },
    {
        value: '关门中'
    },
    {
        value: '暂停营业'
    }
];

// 取消
const doCancel = () => {
    // 触发事件：关闭 dialog
    emits("closeDialog");
};

// 新增
const doSave = async () => {
    const { data } = await axios.post('/api/shangjia', shangjia.value);
    console.log(data);
    if (data.code === 200) {
        emits("flushPage"); // 触发事件： 刷新页面
        emits("closeDialog"); // 触发事件：关闭 dialog
        ElMessage.success("操作成功！");
    } else {
        ElMessage.error("操作失败！");
    }
};
</script>

<style scoped>
.add-shop-form {
    padding: 20px;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.button-row {
    margin-top: 20px;
}

.button-container {
    display: flex;
    justify-content: center;  /* 水平居中 */
    gap: 15px;              /* 按钮间距 */
}

/* 响应式调整 */
@media (max-width: 768px) {
    .add-shop-form {
        padding: 15px;
    }

    .el-col {
        width: 100% !important;
    }
}

</style>